
#include "deal_http_post_details.h"
#include <iostream>
#include <QJsonArray>

DealHttpPostDetails::DealHttpPostDetails(QObject *parent):QObject(parent)
{
    connect(&CHttpSupport::instance(), &CHttpSupport::httpPostRspReady, this, &DealHttpPostDetails::DealPostBackData);
    connect(&CHttpSupport::instance(), &CHttpSupport::httpGetRspReady, this, &DealHttpPostDetails::DealGetBackData);
}

QPair<QString,DealHttpPostDetails::SignalFunction> DealHttpPostDetails::GetMapValue(const QString& key)
{
    static const QMap<QString, QPair<QString,SignalFunction>> map{
            {"Update",                  {"https://chipslightai.top/update/"
                                                ,&DealHttpPostDetails::updateCallBack}},
            {"Goods",                  {"http://test.chipslightai.com/api/commodity/update/?page=1&size=10&status=2"
                                                ,&DealHttpPostDetails::goodsCallBack}},
            {"Advertisement",          {"http://nj.chipslightai.top/api/advertisement/?status=true"
                                                ,&DealHttpPostDetails::advertisementCallBack}},
            {"PicNews",          {"http://test.chipslightai.com/api/advertisement/new_image/"
                                                ,&DealHttpPostDetails::picNewsCallBack}},
            {"TextNews",          {"http://test.chipslightai.com/api/advertisement/new/"
                                                ,&DealHttpPostDetails::textNewsCallBack}},};
    auto it = map.constBegin();
    while(it != map.constEnd())
    {
        if(it.key() == key || it.value().first == key)
        {
            return it.value();
        }
        ++it;
    }
    return {};
}

QString DealHttpPostDetails::GetUrl(const QString& key)
{
    return GetMapValue(key).first;
}

DealHttpPostDetails::SignalFunction DealHttpPostDetails::GetFunc(const QString& key)
{
    return GetMapValue(key).second;
}

DealHttpPostDetails & DealHttpPostDetails::instance()
{
    static DealHttpPostDetails instance;
    return instance;
}

void DealHttpPostDetails::DealPostBackData(const QString& url, const QString& postMD5, const QByteArray& data)
{
    auto func = GetFunc(url);
    if(func != nullptr)
    {
        (this->*func)(data);
    }
}

void DealHttpPostDetails::DealGetBackData(QString url, QByteArray data)
{
    auto func = GetFunc(url);
    if(func != nullptr)
    {
        (this->*func)(data);
    }
}

void DealHttpPostDetails::updateCallBack(const QByteArray &data)
{
    QJsonArray array = QJsonDocument::fromJson(data).array();
    emit signalUpdateVersionCheck(array);
}

void DealHttpPostDetails::goodsCallBack(const QByteArray &data)
{
    QJsonObject obj = QJsonDocument::fromJson(data).object();
    emit signalGoods(obj);
}

void DealHttpPostDetails::versionCheck()
{
    QString url = GetUrl("Update");
    CHttpSupport::instance().httpGet(url);
}

void DealHttpPostDetails::getGoods()
{
    QString url = GetUrl("Goods");
    CHttpSupport::instance().httpGet(url);
}

void DealHttpPostDetails::Advertisement()
{
    QString url = GetUrl("Advertisement");
    CHttpSupport::instance().httpGet(url);
}

void DealHttpPostDetails::advertisementCallBack(const QByteArray& data)
{
    QJsonObject obj = QJsonDocument::fromJson(data).object();
    emit signalAdvertisement(obj);
}

void DealHttpPostDetails::getTextNews()
{
    QString url = GetUrl("TextNews");
    CHttpSupport::instance().httpGet(url);
}

void DealHttpPostDetails::getPicNews()
{
    QString url = GetUrl("PicNews");
    CHttpSupport::instance().httpGet(url);
}

void DealHttpPostDetails::picNewsCallBack(const QByteArray &data)
{
    QJsonObject obj = QJsonDocument::fromJson(data).object();
    emit signalPicNews(obj);
}

void DealHttpPostDetails::textNewsCallBack(const QByteArray &data)
{
    QJsonObject obj = QJsonDocument::fromJson(data).object();
    emit signalTextNews(obj);
}
